<?php
	// On prolonge la session
	session_start();
	$id_anim='';
	$code='';
	require_once dirname(__FILE__) . "/../../includes/config.php";
	require_once PHP_ROOTPATH.'/classes/database.php';
	$idconnect=Database::getInstance();
		// On teste si la variable de session existe et contient une valeur
		if(empty($_SESSION['id_anim'])){
			// Si inexistante ou nulle, on redirige vers la page de saisie du code
			header('Location: ../recherche.php');
			exit();
		}
		else{
			$id_anim=$_SESSION['id_anim'];
			$code=$_SESSION['code'];
		}
	
	
	
	  $querytest = pg_query($idconnect,"SELECT droit_utilisateur.id_user, droit_utilisateur.id_droit
  									  FROM droit_utilisateur,droit
 									  where droit_utilisateur.id_droit=droit.id_droit and  droit.type='visualisation' and  droit_utilisateur.id_user =".$_SESSION['membre_id']);
		$lecture_tout = pg_num_rows($querytest);
		$querytest = pg_query($idconnect,"SELECT droit_utilisateur.id_user, droit_utilisateur.id_droit
  									  FROM droit_utilisateur,droit
 									  where droit_utilisateur.id_droit=droit.id_droit and  droit.type='saisie' and  droit_utilisateur.id_user =".$_SESSION['membre_id']);
		$droit_saisie = pg_num_rows($querytest);
?>

<!DOCTYPE html>
<html>
	<head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="../../style.css" />
		<!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <!--[if lte IE 7]>
        <link rel="stylesheet" href="../../style_ie.css" />
        <![endif]-->
        <title>Base de données microcèbes</title>
        
        <script type="text/javascript">
			function afficher(oSelect){
				var id=oSelect.id;
				var elt = document.getElementById(id);
				if(elt.style.display=="inline-block"){
					elt.style.display="none";
				}
				else{
					elt.style.display="inline-block";
				}
			}
		</script>
    </head>
    
    <body>
		<?php include((dirname(dirname(dirname(__FILE__))))."/menu.php"); ?>
		<?php include((dirname(dirname(__FILE__)))."/calcul_age.php"); ?>
		
		<section id="main">
			<header>
			<?php if($lecture_tout != 0){ ?>
				<h1>Recherche - Séquençage animal <?php echo $code;?></h1>
			<?php } else{ ?>
				<h1>Recherche - Génotypage/Séquençage animal <?php echo $code;?></h1>
			<?php } ?>
			</header>
			<?php include((dirname(dirname(__FILE__)))."/menu_recherche.php"); ?>
			<section id="animal">
				<div id="menu3">
					<ul id="onglets">
						<?php if($lecture_tout == 7 && $droit_saisie > 0){ ?>
							<li><a href="genotype.php">Génotypage</a></li>
							<li class="active"><a href="seq.php">Séquençage</a></li>
						<?php } ?>
					</ul>
				</div>
				<section id="contenu">
					<?php                 
						
						$query=pg_query($idconnect,"SELECT sequencage_adn.id_res,date_test,experimentateur,entier
						FROM sequencage_adn,resultat_test,test,type_test 
						WHERE label LIKE '%Séquencage%' AND id_animal='".$id_anim."'
						AND sequencage_adn.id_res=resultat_test.id_res
						AND resultat_test.id_test=test.id_test AND test.id_type_test=type_test.id_type_test");
						//informations sequencage_adn
						$int = pg_num_rows($query);
						if($int==0){echo("<p><label>Pas d'informations disponibles</label></p>");}
						else{
							while ($back = pg_fetch_assoc($query)) {														
								//informations sur les gènes
								$query2=pg_query($idconnect,"SELECT * from gene where gene.id_res=".$back["id_res"]);
								while ($back2 = pg_fetch_assoc($query2)) {
									if($back2["nom_gene"]){ 
										echo "<div><label class=\"titre2\">Gène ".$back2["nom_gene"]."</label><input type=\"button\" onclick=\"afficher(gene".$back2["nom_gene"].")\" value=\"Détail\"></div>";
										echo "<section class='autre' id=\"gene".$back2["nom_gene"]."\"><p>";
										if($back["entier"]=="t"){echo "<label><strong>Séquençage entier: </strong>Oui</label><br/>";}
										elseif($back["entier"]=="f"){echo "<label><strong>Séquençage entier: </strong>Non</label><br/>";}	
										if($back["date_test"]){
											echo "<label><strong>Date du test: </strong>".date("d/m/Y", strtotime($back["date_test"]))."</label><br/>";
											echo "<label><strong>Age (mois): </strong>".calcul_age($_SESSION['date_n'],$back["date_test"])."</label><br/>";
											echo "<label><strong>Age (cycle): </strong>".calcul_age($_SESSION['date_n'],$back["date_test"])/8;
											echo "</label><br/>";
											}
										if($back["experimentateur"]){echo "<label><strong>Expérimentateur: </strong>".$back["experimentateur"]."</label><br/>";}							
										
										//informations snp sous forme d'un tableau
										echo "<label class='code4' onclick=\"afficher(liste_snp".$back2["nom_gene"].")\"><strong>Liste des SNP trouvés</strong></label><br/>";
										echo "<label class='code4' onclick=\"afficher(liste_mut".$back2["nom_gene"].")\"><strong>Liste des Mutations trouvées</strong></label>";
										echo "</p>";
										echo "<section class=\"partie1\" id=\"liste_snp".$back2["nom_gene"]."\">";
										echo "<p><CAPTION><strong>SNP:</strong></CAPTION></p>";
										echo "<TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"3\">";
										echo "<TR ALIGN=\"CENTER\">";
										echo "<TH> Localisation </TH>";
										echo "<TH> Type de variation </TH>";
										echo "<TH> Résultat </TH>";
										echo "</TR>"; 
										$query3 = pg_query($idconnect,"SELECT * from snp where snp.id_res=".$back2["id_res"]. "AND snp.nom_gene='".$back2["nom_gene"]."'");
										while ($back3 = pg_fetch_assoc($query3)) {
											echo "<TR ALIGN=\"CENTER\">";
											if($back3["localisation"]){echo "<TD>".$back3["localisation"]."</TD>";}else{echo "<TD></TD>";}
											if($back3["type_variation"]){echo "<TD>".$back3["type_variation"]."</TD>";}else{echo "<TD></TD>";}		
											if($back3["valeur_res"]==0){echo "<TD>Sauvage</TD>";}
											else if($back3["valeur_res"]==1){echo"<TD>Hétérozygte</TD>";}
											else if($back3["valeur_res"]==2){echo "<TD>Homozygote</TD>";}
											else{echo "<TD></TD>";}
											echo "</TR>"; 
										}
										echo "</TABLE></section>";

										//informations mutation sous forme d'un tableau
										echo "<section class=\"partie1\" id=\"liste_mut".$back2["nom_gene"]."\">";
										echo "<p><CAPTION><strong>Mutations:</strong></CAPTION></p>";
										echo "<TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"3\">";
										echo "<TR ALIGN=\"CENTER\">";
										echo "<TH> Localisation </TH>";
										echo "<TH> Type de variation </TH>";
										echo "<TH> Type de mutation </TH>";
										echo "<TH> Résultat </TH>";
										echo "</TR>"; 
										$query3 = pg_query($idconnect,"SELECT * from mutation where mutation.id_res=".$back2["id_res"]. "AND mutation.nom_gene='".$back2["nom_gene"]."'");
										while ($back3 = pg_fetch_assoc($query3)) {
											echo "<TR ALIGN=\"CENTER\">";
											if($back3["localisation"]){echo "<TD>".$back3["localisation"]."</TD>";}else{echo "<TD></TD>";}
											if($back3["type_variation"]){echo "<TD>".$back3["type_variation"]."</TD>";}else{echo "<TD></TD>";}		
											if($back3["type_mutation"]){echo "<TD>".$back3["type_mutation"]."</TD>";}else{echo "<TD></TD>";}	
											if($back3["valeur_res"]==0 or $back3["valeur_res"]==1 or $back3["valeur_res"]==2){echo "<TD>".$back3["valeur_res"]."</TD>";}else{echo "<TD></TD>";}
											echo "</TR>"; 
										}
										echo "</TABLE></section>";
									}
								}
								echo "</section>";
							}
						}
					?>
				</section>
			</section>
		</section>
	</body>
</html>
